package org.geektimes.configuration.microprofile.config.source;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

/**
 * @author karidyang
 */
public class LocalFilePropertiesConfigSource extends MapPropertiesSource {

    private final static String PROPERTIES_FILE = "META-INF/application.properties";

    public LocalFilePropertiesConfigSource() {
        super("Local File Properties", 600);
    }

    @Override
    public void readProperties(Map properties) throws Throwable {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource(PROPERTIES_FILE);
        if (url == null) {
//            throw new NoSuchFileException(PROPERTIES_FILE + " not Found ");
            return;
        }

        try (InputStream in = new FileInputStream(new File(url.getFile()))) {
            Properties propertiesFile = new Properties();
            propertiesFile.load(in);
            properties.putAll(propertiesFile);
        }catch (IOException e) {
        }

    }
}
